# encoding: utf-8
class ProjectsController < ApplicationController
  before_filter :authenticate_user!, :only => [:show, :new, :delete, :edit]
  before_filter :is_member_of_project?, :only => [:edit, :update, :destroy]

  def index
    if user_signed_in?
      @projects = Project.where("id in (?)", User.where(:id => current_user.id).first.projects)
    else
      @projects = Project.all
    end
    if params[:all_projects] == "y"
      @projects = Project.all
    end
    respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @projects }
    end
  end

  def show
    @project = Project.where(:id => params[:id]).first
    session[:current_project_id] = @project.id

    @project_members = @project.users

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end

  def new
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @project }
    end
  end

  def edit
    if is_project_manager?
      @project = Project.find(params[:id])
    else
      flash[:error] = t :project_no_right_edit, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to( project_path(params[:project_id]) )} # params[:id]
        format.xml { render :xml => @project }
      end
    end
  end

  def create
    @project = Project.new(params[:project])
    ActiveRecord::Base.transaction do
      respond_to do |format|
        if @project.save && Member.create(  :project_id =>  @project.id,
                                            :user_id    =>  current_user.id,
                                            :role       =>  'manager',
                                            :status     =>  "available",
                                            :load       =>  1)
          flash[:notice] = t :project_create_success, :scope => [:flash, :notice]
          format.html { redirect_to(@project) }
          format.xml  { render :xml => @project, :status => :created, :location => @project }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  def update
    @project = Project.find(params[:id])

    respond_to do |format|
      if @project.update_attributes(params[:project])
        flash.now[:notice] = t :project_edit_success, :scope => [:flash, :notice]
        format.html { redirect_to(@project) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @project = Project.where(params[:id]).first
    if is_project_manager?
      @project.destroy
      respond_to do |format|
        format.html { redirect_to(projects_url) }
        format.xml  { head :ok }
      end
    else
      flash[:error] = t :project_no_right_delete, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to( project_path(params[:project_id]) )}
        format.xml { render :xml => @project }
      end
    end
  end
end
